﻿#include "GUIMaterialPropDialog.h"
#include "ui_GUIMaterialPropDialog.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Component/FITKRadiossData/FITKRadiossCase.h"
#include "FITK_Component/FITKWidget/FITKSciNotationLineEdit.h"

#include <QComboBox>

namespace GUI
{
    GUIMaterialPropDialog::GUIMaterialPropDialog(Interface::FITKAbstractMaterial* obj, Core::FITKActionOperator* oper, QWidget* parent)
        : Core::FITKDialog(parent), _obj(obj), _oper(oper),_ui(new Ui::GUIMaterialPropDialog)
    {
        _ui->setupUi(this);
        //获取RadiossCase
        Radioss::FITKRadiossCase* caseObj = FITKAPP->getGlobalData()->
            getPhysicsData<Radioss::FITKRadiossCase>();
        if (caseObj == nullptr) return;
        //获取材料管理器
        _matMgr = caseObj->getMaterialManager();
        if (!_obj) 
            _isCreate = true;
        // 设置对话框关闭时自动删除
        setAttribute(Qt::WA_DeleteOnClose);
        //去掉问号
        setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    }

    GUIMaterialPropDialog::~GUIMaterialPropDialog()
    {
        if (_ui) delete _ui;
        _ui = nullptr;
    }

    bool GUIMaterialPropDialog::createObjData()
    {
        if (!_matMgr) return false;
        if(!editObjData())return false;
        _matMgr->appendDataObj(_obj);
        return true;
    }

    bool GUIMaterialPropDialog::editObjData()
    {
        if (!_oper || !_obj|| !_matMgr) return false;
        if(!this->ckeckData()) return false;

        //设置名称
        _obj->setDataObjectName(_ui->lineEdit_name->text());
        //设置界面数据
        this->getDataFormWidget();
 
        _oper->setArgs("objID", _obj->getDataObjectID());
        return true;
    }

    bool GUIMaterialPropDialog::ckeckData()
    {
        //虚函数重写
        return true;
    }

    QTreeWidgetItem* GUIMaterialPropDialog::addChiledItem(QTreeWidgetItem* p, const QString& objName,const QString& label,const QString& unit)
    {
        if (!p)return nullptr;
        QWidget* widget = new QWidget(this);
        QHBoxLayout* layout = new QHBoxLayout();
        Comp::FITKSciNotationLineEdit* lineEdit = new Comp::FITKSciNotationLineEdit(this);
        lineEdit->setObjectName(objName);
        //// 设置 lineEdit 只能输入数字
        //QDoubleValidator *doubleValidator = new QDoubleValidator(0, 1e30, 9, this);
        //doubleValidator->setNotation(QDoubleValidator::StandardNotation);
        //lineEdit->setValidator(doubleValidator);

        QLabel* qlabel = new QLabel(this);
        qlabel->setText(unit);
        layout->addWidget(lineEdit);
        layout->addWidget(qlabel);
        layout->setStretch(0, 1);
        layout->setStretch(1, 1);
        widget->setLayout(layout);
        QTreeWidgetItem* item = new QTreeWidgetItem(p);
        item->setText(0, label);
        _ui->treeWidget_parameter->setItemWidget(item, 1, widget);
        return item;
    }

    void GUIMaterialPropDialog::on_pushButtonOK_clicked()
    {
        //创建动作
        if (_isCreate)
        {
            if (!this->createObjData())
                return;
            _oper->execProfession();
        }
        //修改动作
        else 
        {
            if (!this->editObjData())
                return;
            _oper->execProfession();
        }
        this->accept();
    }

    void GUIMaterialPropDialog::on_pushButtonCancel_clicked()
    {
        //点击取消按键动作
        this->reject();
    }

    void GUIMaterialPropDialog::reject()
    {
        //关闭窗口
        _oper->setArgs("objID", -1);
        if (_isCreate) {
            //删除对象
            if (_obj) delete _obj;
            _obj = nullptr;
        }
        QDialog::reject();
    }
}